/* 
 * 分享
 */


var exec = require('cordova/exec'), sharesdk = function(){
    this.needInit = true;
};

sharesdk.prototype.native = function(name, args, success){
    //console.log('sharesdk native call: ' + JSON.stringify([name, args]));
    if(typeof (success) !== 'function'){
        success = function(){
        };
    }
    try{
        return exec(function(msg){
            success(null, msg);
        }, function(err){
            success(err, null);
        }, 'ShareSdkPlugin', name, typeof (args) === 'object' ? args : [args]);
    }catch(e){
        console.log('native error: ' + JSON.stringify(e));
    }
};

/**
 * 初始化
 * @param {Function} func
 * @returns {sharesdk.prototype}
 */
sharesdk.prototype.initSDK = function(func){
    if(this.needInit === true){
        this.native('initSDK', [], function(err){
            if(err === null){
                this.needInit = false;
            }
            func.apply(this, arguments);
        }.bind(this));
    }else{
        console.log('already called initSDK!');
        func(null, true);
    }
    return this;
};

/**
 * test wechat
 * @param {Function} func
 * @returns {sharesdk.prototype}
 */
sharesdk.prototype.wechat = function(func){
    this.native('wechat', [], func);
    return this;
};

/**
 * [SinaWeibo, TencentWeibo, QZone, Wechat, WechatMoments, WechatFavorite, QQ, Evernote, Yixin, YixinMoments]
 * @param {String} platform
 * @param {Object} data
 * @param {Function} func
 * @returns {sharesdk.prototype}
 */
sharesdk.prototype.share = function(platform, data, func){
    this.initSDK(function(err){
        if(err === null)
            this.native('share', [platform, data], func);
    }.bind(this));
    return this;
};

module.exports = new sharesdk();